import 'package:environmental_les_app/common/net/net.dart';
import 'package:environmental_les_app/common/utils.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';
import 'package:flutter_clx_base/widget/base_list_widget.dart';

class WarningListController extends GetxController {
  WarningListController() {
    for (final item in filterData) {
      selectedFilters[item['prop'] as String] = [];
    }
  }
  final listController = BaseListController();

  //当前时间筛选结果
  Map<String, dynamic> allTime = {};

  // 当前选中的筛选项，prop -> list of selected values
  final Map<String, List<String>> selectedFilters = {};

  // 筛选项
  final List<Map<String, dynamic>> filterData = [
    {
      "title": "站点类型",
      "prop": "siteType",
      "multiSelect": false,
      "filter": [
        {"name": "生活污水污水", "value": "1"},
        {"name": "工业废水", "value": "2"},
        {"name": "雨水", "value": "3"},
      ],
    },
    {
      "title": "报警类型",
      "prop": "alarmType",
      "multiSelect": false,
      "filter": [
        {"name": "水质报警", "value": "1"},
        {"name": "设备报警", "value": "2"},
        {"name": "视频报警", "value": "3"},
      ],
    },
    {
      "title": "处理状态",
      "prop": "status",
      "multiSelect": true,
      "filter": [
        {"name": "未处理", "value": "1"},
        {"name": "处理中", "value": "2"},
        {"name": "已处理", "value": "3"},
      ],
    },
  ];

  // 时间筛选项
  final List<Map<String, dynamic>> timeFilterData = [
    {
      "title": "报警时间",
      "prop": "alarm",
      "initialStart": DateUtil.formatDate(
          DateTime.now().subtract(const Duration(days: 7)),
          format: "yyyy-MM-dd 00:00"),
      "initialEnd":
          DateUtil.formatDate(DateTime.now(), format: "yyyy-MM-dd 23:59"),
    },
    {
      "title": "处理时间",
      "prop": "handle",
      "initialStart": DateUtil.formatDate(
          DateTime.now().subtract(const Duration(days: 7)),
          format: "yyyy-MM-dd 00:00"),
      "initialEnd":
          DateUtil.formatDate(DateTime.now(), format: "yyyy-MM-dd 23:59"),
    },
  ];

  _initData() {
    update(["warning_list"]);
  }

  // 更新选中结果（来自弹窗返回）
  void updateSelection(Map<String, dynamic>? result) {
    updateSelectedFilters(selectedFilters, filterData, result);
    update(["warning_list"]);
  }

  void getList(page, pageSize, success, error) {
    DioUtils.instance.getMockAdapter().onGet(
          ServiceApi.warningList,
          (server) => server.reply(
              200,
              {
                "data": {
                  "records": List.generate(5, (index) {
                    return {
                      "title": "标题 $index",
                      "subtitle": "这是第 $index 条数据",
                    };
                  }),
                },
                "msg": "success",
                "code": 0
              },
              delay: const Duration(seconds: 1)),
        );

    var queryParameters = {
      "page": page,
      "pageSize": pageSize,
      "alarmTimeStart": allTime['alarm']?['start'],
      "alarmTimeEnd": allTime['alarm']?['end'],
      "handleTimeStart": allTime['handle']?['start'],
      "handleTimeEnd": allTime['handle']?['end'],
      ...selectedFilters.map((key, value) => MapEntry(key, value.join(','))),
    };
    logger.d("请求参数: $queryParameters");
    logger.d("选中筛选项: $selectedFilters");

    NetUtils.get(ServiceApi.warningList, queryParameters: null,
        successCallback: (data) {
      success(data['records']);
    }, errorCallback: error);
  }

  // @override
  // void onInit() {
  //   super.onInit();
  // }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
